
package com.zdkj.subway.st.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.subway.st.dto.in.SwTkPlanTempDecomposeDTO;
import com.zdkj.subway.st.entity.SwStPlanTemp;
import com.zdkj.subway.st.mapper.SwStPlanTempMapper;
import com.zdkj.subway.st.service.SwStPlanTempService;
import com.zdkj.subway.st.util.SubwayStConstants;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;


@Service
@AllArgsConstructor
public class SwStPlanTempServiceImpl extends ServiceImpl<SwStPlanTempMapper, SwStPlanTemp> implements SwStPlanTempService {


    @Override
    public int decompose(SwTkPlanTempDecomposeDTO planTempDecomposeDTO) {
        SwStPlanTemp planTemp = this.getById(planTempDecomposeDTO.getId());
        if(SubwayStConstants.COMPOSE_STATUS_COMFIRM.equals(planTemp.getIsDecompose())){
            return 0;
        }
        planTemp.setIsDecompose(SubwayStConstants.COMPOSE_STATUS_COMFIRM);
        this.updateById(planTemp);
        List<SwStPlanTemp> decomposeList = planTempDecomposeDTO.getDecomposeList();
        decomposeList.forEach(plan -> {
            plan.setNumbers(planTemp.getNumbers());
            plan.setPlanType(planTemp.getPlanType());
            plan.setIsSend(planTemp.getIsSend());
            plan.setCheckStatus(planTemp.getCheckStatus());
            plan.setTicketType(planTemp.getTicketType());
            plan.setTicketName(planTemp.getTicketName());
            plan.setTicketPrice(planTemp.getTicketPrice());
            plan.setTicketTimes(planTemp.getTicketTimes());
            plan.setLatestStartTime(planTemp.getLatestStartTime());
            plan.setActiveTime(planTemp.getActiveTime());
            plan.setTicketPurpose(planTemp.getTicketPurpose());
        });
        saveBatch(decomposeList);
        return 1;
    }

    /**
     * 车票临时计划配送信息模板下载
     * @param response
     */
    @Override
    public void exportTamplateDeliveryTemp(HttpServletResponse response) {
        String  verifyConfirmName = "车票临时计划配送信息模板";
        String  tableName = "sw_tk_public_plan_temp";

        BufferedInputStream br = null;
        OutputStream out = null;
        try {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            final String fileName = URLEncoder.encode(verifyConfirmName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");

            br =  new BufferedInputStream(this.getClass().getResourceAsStream("/static/excel/template/import/" + tableName + ".xlsx"));

            byte[] buf = new byte[1024];
            int len = 0;

            out = response.getOutputStream();
            while ((len = br.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }

	/**
     * 临时日计划模板下载
     * @param response
     */
    @Override
    public void exportTamplatePlan(HttpServletResponse response){
        String  verifyConfirmName = "临时日计划模板";
        String  tableName = "sw_tk_public_plan_day_temp";

        BufferedInputStream br = null;
        OutputStream out = null;
        try {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            final String fileName = URLEncoder.encode(verifyConfirmName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");

            br =  new BufferedInputStream(this.getClass().getResourceAsStream("/static/excel/template/import/" + tableName + ".xlsx"));

            byte[] buf = new byte[1024];
            int len = 0;

            out = response.getOutputStream();
            while ((len = br.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }
}
